package com.xinyue.auto.sql.util;

import com.xinyue.auto.type.MySQL;
import com.xinyue.auto.type.Type;
import com.xinyue.auto.util.StringUtil;

public class MysqlUtil implements Type, MySQL{
	/**
	 * 将数据组装成sql语句
	 * @param type
	 * @return
	 */
	public static String buildSql(String name, String type, String len, boolean notNull, boolean unique) {
		String sql = name + " ";
		
		if(STRING.equals(type)) {
			if(StringUtil.isNull(len)) {
				len = "255";
			}
			sql += _VARCHAR + "(" + len + ")";
		}else if(LONG.equals(type)) {
			sql += _BIGINT;
			notNull = true;
		}else if(INT.equals(type)) {
			sql += _INTEGER;
			notNull = true;
		}else if(DOUBLE.equals(type)) {
			sql += _DOUBLE;
			notNull = true;
		}else if(BOOLEAN.equals(type)) {
			sql += _BOOLEAN;
			notNull = true;
		}else if(DATE.equals(type)) {
			sql += _DATETIME;
		}else if(BIGDECIMAL.equals(type)) {
			sql += _DECIMAL + "(19,2)";
		}else if(BLOB.equals(type)) {
			sql += _LONGBLOB;
		}else if(CHAR.equals(type)) {
			sql += _CHAR + "(1)";
			notNull = true;
		}else if(BYTE.equals(type)) {
			sql += _TINYINT;
			notNull = true;
		}else if(SHORT.equals(type)) {
			sql += _SMALLINT;
			notNull = true;
		}else if(FLOAT.equals(type)) {
			sql += _FLOAT;
			notNull = true;
		}else {
			//自定义类型
			sql += type;
		}
		
		if(notNull) {
			sql += " not null";
		}
		if(unique) {
			sql += " unique";
		}
		
		return sql;
	}
}
